package qct.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.mysql.jdbc.StringUtils;

public class DateUtil {
	public static Date String2Date(String dateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date addOneDay(Date oldDate) {
		Calendar c = Calendar.getInstance();
		c.setTime(oldDate);
		c.add(Calendar.DAY_OF_MONTH, 1);
		return c.getTime();
	}
	
	public static Date String2Date(String dateStr, String formatter) {
		SimpleDateFormat sdf = null;
		if(!StringUtils.isNullOrEmpty(formatter)) {
			sdf = new SimpleDateFormat(formatter);
		}else {
			sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		}
		try {
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static java.sql.Date String2SqlDate(String dateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return new java.sql.Date(sdf.parse(dateStr).getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 比较两个日期相差的秒数
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int getTimeDifference(Date date1, Date date2) {
		return (new Long(date1.getTime()-date2.getTime()).intValue())/1000;
	}
	
	public static String transTimestamp2String(Timestamp timestamp, String formatter) {
		SimpleDateFormat sdf = null;
		if(!StringUtils.isNullOrEmpty(formatter)) {
			sdf = new SimpleDateFormat(formatter);
		}
		if(sdf != null) {
			return sdf.format(timestamp.getTime());
		}
		return timestamp.toString();
	}
	
	public static String transTimestamp2String(Timestamp timestamp) {
		if(timestamp == null) return "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return sdf.format(timestamp.getTime());
	}
	
	public static String convertDateFormat(String date, String formatter) {
		SimpleDateFormat sdf = null;
		if(!StringUtils.isNullOrEmpty(formatter)) {
			sdf = new SimpleDateFormat(formatter);
		}else {
			sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		}
		
		Date newDate = null;
		try {
			newDate = sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return sdf.format(newDate);
	}
	
	public static String formatDate(String formatter, Date date) {
		SimpleDateFormat sdf = null;
		if (!StringUtils.isNullOrEmpty(formatter)) {
			sdf = new SimpleDateFormat(formatter);
		} else {
			sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		}
		return sdf.format(date.getTime());
	}
	
	public static Date formatDate2Date(String formatter, Date date) throws ParseException {
		SimpleDateFormat sdf = null;
		if (!StringUtils.isNullOrEmpty(formatter)) {
			sdf = new SimpleDateFormat(formatter);
		} else {
			sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		}
		return sdf.parse(sdf.format(date));
	}
}
